﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using MahApps.Metro.Controls;
using System.Web.Security;
using System.Reflection;
using MahApps.Metro.Controls.Dialogs;

namespace GroepEF4
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : MetroWindow
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Grid_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                MembershipCreateStatus result;
                //int total;

                //Default Admin aanmaken..
                MembershipUser newUser = Membership.CreateUser("Admin",
                                                          "Admin!",
                                                          "admin@admin.com",
                                                          "wie ben ik?",
                                                          "Admin",
                                                          true,
                                                          out result);

                //Default User aanmaken
                MembershipUser newUser2 = Membership.CreateUser("User",
                                                          "User!",
                                                          "user@user.com",
                                                          "wie ben ik?",
                                                          "User",
                                                          true,
                                                          out result);

                if (!Roles.RoleExists("Admin"))//controle of rol admin al bestaat
                    Roles.CreateRole("Admin");//indien nog niet.. rol admin aanmaken
                if (!Roles.IsUserInRole("Admin", "Admin"))//controle of de gebruiker de rol admin heeft.
                    Roles.AddUserToRole("Admin", "Admin");//indien niet krijgt deze de rol admin.

                if (!Roles.RoleExists("User"))//controle of rol user al bestaat
                    Roles.CreateRole("User");//indien nog niet.. rol user aanmaken
                if (!Roles.IsUserInRole("User", "User"))//controle of de gebruiker de rol user heeft.
                    Roles.AddUserToRole("User", "User");//indien niet krijgt deze de rol user.
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }

        public async void Button_Click(object sender, RoutedEventArgs e)
        {          
            MembershipCreateStatus result;
            if (txtGebruikersnaam.Text == "")
            {
                await this.ShowMessageAsync("Fout!", "Gebruikersnaam moet ingevuld worden.");
            }
            else if (txtPassword.Password == "")
            {
                await this.ShowMessageAsync("Fout!", "Wachtwoord moet ingevuld worden.");
            }
            else if (Membership.ValidateUser(txtGebruikersnaam.Text, txtPassword.Password))
            {
                ProgressRing.IsActive = true;
                string username = txtGebruikersnaam.Text;

                MembershipUser currentUser = Membership.GetUser(username);
                string userId = currentUser.ProviderUserKey.ToString();

                if (Roles.IsUserInRole(txtGebruikersnaam.Text, "Admin"))
                {
                    AdminWindow aw = new AdminWindow();
                    aw.Show();
                    this.Close();
                }
                else if (Roles.IsUserInRole(txtGebruikersnaam.Text, "User"))
                {
                    Algemeen algemeen = new Algemeen(userId);
                    algemeen.Show();
                    this.Close();
                }
            }
            else
            {
                await this.ShowMessageAsync("Fout!", "Foutieve gebruikersnaam en/of wachtwoord.");
            }
        }
    }
}
